package com.edou.auth.tag;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.edou.auth.entity.Right;
import com.edou.base.constant.SessionConstant;

/**
 * 权限函数，判断当前用户是否有该操作权限
 * @creator     admin
 * @create-time 2017年3月3日   下午6:27:00
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
public class CheckAuthTag{
	@SuppressWarnings("unused")
	private static final long serialVersionUID = -8314365112353913892L;
	
	@SuppressWarnings("unchecked")
	public static Boolean checkAuth(String pcode){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); 
		HttpSession session = request.getSession(false);
		//获取菜单列表
		List<Right> rights = (List<Right>) session.getAttribute(SessionConstant.CURRENT_PRIVILEGE);
		
		if(StringUtils.isNotBlank(pcode)){
			for(Right curRight : rights){
				if(StringUtils.equals(curRight.getPcode(), pcode)){
					return true;
				}
			}
		}
		return false;
	}
}
